package pro.cherish.knowledgewebflux.util;

import java.util.HashMap;
import java.util.Map;

public class ThreadContextHandler {

    private static final ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>() {
        @Override
        protected Map<String, Object> initialValue() {
            return new HashMap();
        }
    };

    public ThreadContextHandler() {
    }

    public static Map<String, Object> getThreadLocal() {
        return (Map)threadLocal.get();
    }

    public static Object get(String key) {
        Map<String, Object> map = (Map)threadLocal.get();
        return map.get(key);
    }

    public static Object get(String key, Object defaultValue) {
        Map<String, Object> map = (Map)threadLocal.get();
        return map.get(key) == null ? defaultValue : map.get(key);
    }

    public static void set(String key, Object value) {
        ((Map)threadLocal.get()).put(key, value);
    }

    public static void set(Map<String, Object> keyValueMap) {
        ((Map)threadLocal.get()).putAll(keyValueMap);
    }

    public static void clear() {
        threadLocal.remove();
    }

    public static Object removeValue(String key) {
        return ((Map)threadLocal.get()).remove(key);
    }
}
